﻿using DBUtil.Provider.MySql;
using log4net;
using Sunny.UI;
using System.Configuration;

namespace FZRD
{
    public partial class LoginForm : UILoginForm
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(LoginForm));

        public static string username = "";

        public static string password = "";

        private bool IsAdmin = false;

        public static MySqlAccess db;

        public LoginForm()
        {
            InitializeComponent();

            string mySqlDBUrl = ConfigurationManager.AppSettings["MySqlDBUrl"];

            DBUtil.DBFactory.AddDBSupport<DBUtil.Provider.MySql.MySqlDBFactory>();
            db = MySqlDBFactory.CreateDB(mySqlDBUrl,
                DBUtil.DBSetting.NewInstance().SetSqlMonitor(arg =>
                {
                    log.Info(arg);
                    return Task.CompletedTask;
                })
            );
        }

        private void FLogin_ButtonLoginClick(object sender, System.EventArgs e)
        {
            try
            {
                var sql = @"select * from system_user where UserName = '" + UserName + "' and Password = '" + Password + "'";
                var dt = db.SelectDataTable(sql);
                if (dt.Rows.Count < 1) this.ShowInfoTip("用户名或密码错误!");
                username = UserName;
                password = Password;
                int GroupID = dt.Rows[0]["GroupID"] != DBNull.Value ? (int)dt.Rows[0]["GroupID"] : 0; 
                int Permission = dt.Rows[0]["Permission"] != DBNull.Value ? (int)dt.Rows[0]["Permission"] : 0;
                if (Permission == 1)
                {
                    IsAdmin = true;
                }
                IsLogin = true;
                string userIp = "127.0.0.1";
                long recordTime = ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeMilliseconds();

                db.ExecuteSql(
                    db.Insert("system_operation_content")
                        .SetColumn("UserName", UserName)
                        .SetColumn("UserIP", userIp)
                        .SetColumn("RecordTime", recordTime)
                        .SetColumn("Operation", "登录")
                        .ToSql());
                db.ExecuteSql(
                    db.Update("system_user")
                                .SetColumn("LoginTime", recordTime)
                                .Where("UserName='" + username + "'")
                                .ToSql());
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
            Close();
            //if ((UserName == this.username) && (Password == this.password))
            //{
            //    IsLogin = true;
            //    IsAdmin = true;
            //    Close();
            //}
            //else if ((UserName == this.username1) && (Password == this.password1))
            //{
            //    IsLogin = true;
            //    Close();
            //}
            //else
            //{
            //    this.ShowInfoTip("用户名或密码错误!");
            //}
        }

        public bool GetIsAdmin()
        {
            return IsAdmin;
        }
    }
}